package com.sand.controller;

import com.alibaba.fastjson.JSON;
import com.sand.entity.TRoleEntity;
import com.sand.entity.TUserEntity;
import com.sand.service.IRoleService;
import com.sand.util.ModelTest;
import com.sand.util.PageModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping(value = "role")
public class RoleController {
/**
 *
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/11/29/029 11:06
 */
    //roleService接口
    @Resource(name = "roleService")
    private IRoleService roleService;

    //分页查询角色
    @RequestMapping(value = "/queryRoleLimit")
    public void queryRoleLimit(int limit, int page, String condition, HttpServletRequest request, HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");

        PageModel<TRoleEntity> pm = roleService.queryRoleLimit(limit, page, condition);
        ModelTest<List<TRoleEntity>> listData = new ModelTest<List<TRoleEntity>>("OK",pm.getList(),0, pm.getPagecount());
        response.getWriter().write(JSON.toJSON(listData).toString());
    }
    //删除角色
    @RequestMapping(value = "/delRole")
    public void delRole(String data,HttpServletResponse response)throws Exception{
        boolean bln = roleService.delRole(data);
        if(bln){
            response.getWriter().write("OK");
        }else{
            response.getWriter().write("NO");
        }
    }
    //添加角色
    @RequestMapping(value = "/addRole")
    public void addRole(String name,HttpServletResponse response)throws Exception{
        boolean bln = roleService.addRole(name);
        if(bln){
            response.getWriter().write("OK");
        }else{
            response.getWriter().write("NO");
        }
    }
    //根据id查询角色
    @RequestMapping(value = "/queryRoleById")
    public ModelAndView queryRoleById(int rid){
        TRoleEntity role = roleService.queryRoleById(rid);
        return new ModelAndView("/layui/page/table/edit_role").addObject("upRole",role);
    }
    //添加角色
    @RequestMapping(value = "/updateRole")
    public void updateRole(String data,HttpServletResponse response)throws Exception{
        boolean bln = roleService.updateRole(data);
        if(bln){
            response.getWriter().write("OK");
        }else{
            response.getWriter().write("NO");
        }
    }
    //修改角色拥有的权限
    @RequestMapping(value = "/updateRoleAndAuth/{id}")
    public void updateRoleAndAuth(@PathVariable int id, String data,HttpServletResponse response)throws Exception{
        boolean bln = roleService.updateRoleAndAuthority(id, data);
        if(bln){
            response.getWriter().write("OK");
        }else{
            response.getWriter().write("NO");
        }
    }


}
